#import "BWColorset.h"
#import "BWThreadColor.h"

@implementation BWColorset

- (id) init
{
    if (self = [super init]) {
        colors = [[NSMutableArray alloc] init];
    }

    return (self);

} // init


- (void) dealloc
{
    [manufacturerName release];
    [colors release];

    [super dealloc];

} // dealloc


- (void) setManufacturerName: (NSString *) name
{
    [manufacturerName release];
    manufacturerName = [name copy];

    // and update the name of all the enclosed colors
    NSEnumerator *enumerator = [colors objectEnumerator];

    BWThreadColor *color;
    while (color = [enumerator nextObject]) {
        [color setManufacturerName: name];
    }

} // setManufacturerName


- (NSString *) manufacturerName
{
    return (manufacturerName);

} // manufacturerName



- (int) count
{
    int count;
    count = [colors count];

    return ([colors count]);

} // count



- (BWThreadColor *) colorAtIndex: (int) index
{
    BWThreadColor *color;
    color = [colors objectAtIndex: index];

    return (color);

} // colorAtIndex



- (void) addColor: (BWThreadColor *) color
{
    [colors addObject: color];
    [color setManufacturerName: manufacturerName];

} // addColor


- (void) encodeWithCoder: (NSCoder *) coder
{
    [coder encodeObject: manufacturerName];
    [coder encodeObject: colors];

} // encodeWithCoder


- (id) initWithCoder: (NSCoder *) decoder
{
    if (self = [super init]) {
        [colors release];
        manufacturerName = [decoder decodeObject];
        [manufacturerName retain];
        
        colors = [decoder decodeObject];
        [colors retain];
    }

    return (self);

} // initWithCoder


- (void) removeColorAtIndex: (int) index
{
    [colors removeObjectAtIndex: index];
} // removeColorAtIndex


- (void) reverseColors
{
    int i;
    int count;
    
    count = [colors count];

    for (i = 0; i < count / 2; i++) {
        [colors exchangeObjectAtIndex: i
                withObjectAtIndex: count - i - 1];
    }

} // reverseColors



- (NSString *) description
{
    NSMutableString *string = [NSMutableString stringWithCapacity: 100];

    [string appendString: @"BWColorset "];
    [string appendFormat: @"(%@)", manufacturerName];
    [string appendFormat: @" %d colors", [colors count]];

    return (string);

} // description

@end // BWColorset
